from common.common import Common

class PowerShellCmdError(Exception):
    pass

class PowerShellCmd(object):
    common = Common()    
    header = None
    
    def run(self, cmd_line):
        if self.header is None:
            raise PowerShellCmdError('no correspondent exchange snap-in is loaded')
        cmd_line = self.header + cmd_line
        cmd = 'powershell -command \"& {{{0}}}\"'.format(cmd_line)
        self.common.log.debug(cmd)
        return self.common.run_program(cmd)		    

class ExchangeConsole(PowerShellCmd):	    

    def _run_ps_cmd(self, cmd, error_class):
        [exit_code, out, err] = self.run(cmd)
        if exit_code is not 0:
            raise error_class('method returns error, see powershell error description: {0}'.format(err))				
        return [exit_code, out, err]
